#!/bin/bash
# Installation MySQL
# https://dev.mysql.com/downloads/

[ ! $(id -u) -eq 0 ]  && {
	echo "Please run this script with root"
	exit 1
}

#函数库名称赋值
FUNCTIONS_NAME="1_pub_funtions_makai"
for i in $FUNCTIONS_NAME ; do
        #获取文件路径
        FUNCTIONS_URL="$(find / -path /proc -prune -o -name ${i} -print)"
        #获取不到路径则报错并退出
        ${FUNCTIONS_URL:?null}
        #加载文件
        . $FUNCTIONS_URL
done

#限制变量取值范围
DEBUG="1"
STEP_THROUGH="1"
if [ $# -gt 0 ] ; then
        case "$1" in
                0|1|2)
                        DEBUG="$1" ;;
        esac
        case "$2" in
                1|2)
                        STEP_THROUGH="$2" ;;
        esac
fi

##debug
# debug=0 关闭调试
# debug=1 开启调试
#       STEP_THROUGH=1 遇到错误时暂停
#       STEP_THROUGH=2 不管是否有错误都暂停
# debug=2 遇到错误就退出
alert() {
        local RET_CODE=$?
        if [ -z "$DEBUG" ] || [ "$DEBUG" -eq 0 ] ; then
                return
        fi

        if [ "$RET_CODE" -ne 0 ] ; then
                echo " Warning: $* failed code of $RET_CODE" >&2
                [ "$DEBUG" -eq 2 ] && exit "$RET_CODE"
                [ "$STEP_THROUGH" = 1 ] && {
                        echo "Press [Enter] to containue" >&2; read x
                }
        fi

        [ "$STEP_THROUGH" = 2 ] && {
                echo "Press [Enter] to continue" >&2; read x
        }
}


##二进制安装MySQL
        
	#默认MySQL版本
        DEFAULT_MYSQL_VERSION="mysql-5.7.44-linux-glibc2.12-x86_64"
        MYSQL_VERSION=${MYSQL_VERSION:-$DEFAULT_MYSQL_VERSION}

	#默认安装路径
	DEFAULT_INSTALL_URL="/usr/local"
	INSTALL_URL=${INSTALL_URL:-$DEFAULT_INSTALL_URL}
	
	[ ! -d ${INSTALL_URL}/${MYSQL_VERSION} ]
        alert "${INSTALL_URL}/${MYSQL_VERSION} already exists"

#        while [ $# -gt 0 ] ; do
#                case "$1" in
#                        --version)
#                                MYSQL_VERSION="nginx-$2"
#                                shift
#                                ;;
#                        --*)
#                                echo "Illegal option $1"
#                                ;;
#                esac
#                shift $(( $# > 0 ? 1 : 0 ))
#        done

        echo "for ubuntu centos7..."
	echo "This is ${ostype}"
        echo "start install ${MYSQL_VERSION}..."

	#remove software
        which yum &>/dev/null && {
                yum remove mariadb-libs &>/dev/null
		yum clean all &>/dev/null
		for i in libaio-devel libaio1 ; do
			yum install -y $i &>/dev/null
			alert "$i install failed"
		done
	}

	#install software
	which apt &>/dev/null && {
                sudo apt-get clean &>/dev/null
                for i in libncurses5  libnuma1 ; do
                        sudo apt-get install -y $i &>/dev/null
                        alert "$i install failed"
                done
        }

	#add user
        ! id mysql &>/dev/null && {
                useradd -r -s /sbin/nologin mysql &>/dev/null
                if [[ ! $? -eq 0 && ! $? -eq 9 ]] ; then
                        alert 'add user failed'
		fi
        }

	#download
	[ ! -f /tmp/${MYSQL_VERSION}.tar.gz ] && {
		curl -# -L --output /tmp/${MYSQL_VERSION}.tar.gz https://downloads.mysql.com/archives/get/p/23/file/${MYSQL_VERSION}.tar.gz
		alert 'download failed'
	}

	#tar file
	[ ! -d ${INSTALL_URL}/${MYSQL_VERSION} ] && {
		tar -zxf /tmp/${MYSQL_VERSION}.tar.gz -C ${INSTALL_URL} &>/dev/null
		alert 'tar failed'
		chown -R mysql:mysql ${INSTALL_URL}/${MYSQL_VERSION}
	}

	#create symbolic link
	[ ! -h ${INSTALL_URL}/mysql ] && {
		ln -s ${INSTALL_URL}/${MYSQL_VERSION} ${INSTALL_URL}/mysql
	}

	#设置环境变量
	grep "${INSTALL_URL}/mysql/bin" /etc/profile
	if [ $? == 0 ]; then
		source /etc/profile >/dev/null && echo $PATH && mysql -V
	else
		sed -i "\$a export PATH=\"${INSTALL_URL}/mysql/bin:\$PATH\"" /etc/profile
		source /etc/profile &>/dev/null && echo $PATH && mysql -V
	fi

	#初始化数据库
	mkdir -p /data/mysql/3306
	rm -rf /data/mysql/3306/*
	#rm -f /var/lock/subsys/mysql
	mysqld --initialize-insecure --user=mysql --basedir=${INSTALL_URL}/mysql --datadir=/data/mysql/3306/
	alert "init failed"

	#创建配置文件
	cat >/etc/my.cnf <<-EOF
		[mysqld]
		user=mysql
		basedir=${INSTALL_URL}/mysql
		datadir=/data/mysql/3306
		server_id=6
		port=3306
		socket=/tmp/mysql.sock
		[mysql]
		socket=/tmp/mysql.sock
	EOF

	#启动MySQL
	cp ${INSTALL_URL}/mysql/support-files/mysql.server /etc/init.d/mysqld
	systemctl enable --now mysqld

	#查看MySQL监听端口
	ss -tupln | grep Netid && ss -tupln | grep mysql

	#放行端口
	[ $ostype = 'centos.7' ] && {
		get_Firewalld_status
		if [[ $? -eq 0 ]]; then
			firewall-cmd --zone=public --add-port="3306"/"tcp" --permanent 2>/dev/null
			firewall-cmd --reload &>/dev/null
		fi
	}
